//
//  JXLookOtherPlayCtl.m
//  JiaoX
//
//  Created by yinqy on 15/4/13.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXLookOtherPlayCtl.h"
#import "HomeDataTool.h"
#import "HomeTabInfo.h"
#import "Reviews.h"
#import "SelfDataTool.h"
#import "HomeReviewsCell.h"
#import "CustomShowView.h"
#import "MJRefresh.h"
#import "ShareOrderPopView.h"
#import "JXReviewsController.h"
#import "JXPostsDetailCtl.h"
#import "JXNewTopicCtl.h"

@interface JXLookOtherPlayCtl ()<UITableViewDataSource,UITableViewDelegate,ShareOrderPopViewDelegate>
@property (nonatomic,strong)NSMutableArray *reviewsList;
@end

@implementation JXLookOtherPlayCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"看大家怎么玩"];
    
    self.reviewsList = [NSMutableArray array];
    [self.shareBtn setBackgroundColor:[UIColor blackColor]];
    [self.shareBtn setAlpha:0.7];
    [self.shareBtn setImage:IMG(@"ic_share_prod") forState:UIControlStateNormal];
    [self.shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareBtn.layer setCornerRadius:self.shareBtn.width/2];
    [self.shareBtn.layer setMasksToBounds:YES];
    
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.tableView setTableFooterView:[[UIView alloc] init]];
    
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh:)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh:)];
    
    [self.tableView headerBeginRefreshing];
    
}

-(void)headerRefresh:(UIView *)view{
    [self requestDataWitLastTime:@""];
}

-(void)footerRefresh:(UIView *)view{
    HomeReviews *reviews =  [self.reviewsList lastObject];
    [self requestDataWitLastTime:reviews.createTime];
}

-(void)requestDataWitLastTime:(NSString *)lastTime{
    [HomeDataTool getSunSignleWithSunSingleId:@"" productId:self.prodId createTime:lastTime success:^(HomeTabInfo *result) {
        [Utils refreshComplete:self.tableView];
        if (ISBLANK(result.data)) {
            [self inflateDataWithResult:result createTime:lastTime];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
        [Utils refreshComplete:self.tableView];
    }];
}

-(void)inflateDataWithResult:(HomeTabInfo *)result createTime:(NSString *)createTime{
    if (ISBLANK(createTime)) {
        [self.reviewsList removeAllObjects];
    }
    [self.reviewsList addObjectsFromArray:result.reviewsList];
    [self.tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.reviewsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeReviews *reviews= self.reviewsList[indexPath.row];
    HomeReviewsCell *cell = [HomeReviewsCell cellWithTableView:tableView];
    [cell setReviews:reviews];
    if (!ISBLANK(reviews.nickName)) {
        [cell.homeProdView.readNumBtn setTitle:reviews.nickName forState:UIControlStateNormal];
        NSString *genderImg = @"ic_gender_small_unknow";
        //判断是男是女
        if (reviews.userGender==1) {
            genderImg = @"ic_gender_small_man";
        }else if(reviews.userGender==2){
            genderImg = @"ic_gender_small_woman";
        }
        //判断是否是vip
        if (reviews.level) {
            genderImg = [NSString stringWithFormat:@"%@_vip",genderImg];
        }
        [cell.homeProdView.readNumBtn setImage:IMG(genderImg) forState:UIControlStateNormal];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HomeReviewsViewHeight;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeReviews *reviews= self.reviewsList[indexPath.row];
    if (ISBLANK(reviews.nickName)) {
        //跳转到测评
        JXReviewsController *ctl = [JXReviewsController initWithReviewId:reviews.reviewsId title:@"晒单" reviewsTitle:reviews.title shareImgUrl:nil];
        [self.navigationController pushViewController:ctl animated:YES];
    }else{
        //跳转到帖子
        JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithPostId:reviews.reviewsId];
        [self.navigationController pushViewController:ctl animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)shareBtnClick:(UIButton *)btn{
    [self getShareOrderData];
}

//以下是晒单需要的方法

-(void)getShareOrderData{
    JXBaseRequest *request = [[JXBaseRequest alloc]init];
    NSDictionary *dic = [request keyValues];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    [params setObject:@"1" forKey:@"pageIndex"];
    MBProgressHUD *loding = [DialogUtils showLoadingWithView:self.view];
    [loding show:YES];
    [SelfDataTool getShareOrderList:params success:^(ShareProductRes *result) {
        [loding hide:YES];
        ShareOrderPopView *shareOrderView =  [[ShareOrderPopView alloc]init];
        shareOrderView.width = JXScreenW-60;
        [shareOrderView setDelegate:self];
        //设置数据
        [shareOrderView setShareProductData:result];
        //显示在弹出窗体中
        CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:shareOrderView];
        [showView showWithanim:NO];
    } failure:^(NSError *error) {
        [loding hide:YES];
        [DialogUtils showTipsWithView:self.view String:@"加载晒单数据失败"];
    }];
}

-(void)shareOrder:(CartProdSub *)prod{
    JXNewTopicCtl *ctl = [[JXNewTopicCtl alloc]initWithSharedId:KEY_SUNSINGLE_ID prodId:prod.prodId ProdName:prod.prodName];
    [self.navigationController pushViewController:ctl animated:YES];
}

@end
